<template>
  <div class="wrapper">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="search">
          <span>主播名:</span
          ><el-input
            v-model="checkcontent.name"
            placeholder="请输入主播姓名"
          ></el-input>
        </div>
        <div class="block">
          <el-date-picker
            v-model="checkcontent.dateValue"
            type="month"
            placeholder="选择月"
          >
          </el-date-picker>
        </div>
        <div class="buttongroup">
          <el-button type="primary" @click="check">查询</el-button>
        </div>
      </div>
      <div>
        <el-button @click="backtabel">返回</el-button>
      </div>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="id" label="id" width="180"> </el-table-column>
        <el-table-column prop="name" label="名称" width="180">
        </el-table-column>
        <el-table-column prop="profit" label="提成" width="180">
        </el-table-column>
        <el-table-column label="日期">
          <template slot-scope="scope">
            <div>
              {{ scope.row.date | filterDate }}
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="edit(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button type="text" size="small" @click="deleteUser(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage3"
          :page-size="page_size"
          layout="prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="主播id" prop="id">
            <el-input v-model="ruleForm.id" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="主播姓名" prop="name">
            <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="总销售额" prop="profit">
            <el-input v-model="ruleForm.profit" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="日期" prop="date">
            <el-date-picker
              v-model="ruleForm.date"
              type="month"
              placeholder="选择月"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  components: {},
  props: {},
  data() {
    return {
      checkcontent: { dateValue: "", name: "" },
      totalData: [],
      tableData: [],
      page_size: 8,
      total: 0,
      currentPage3: 1,
      dialogVisible: false,
      ruleForm: {
        uid: "",
        id: "",
        name: "",
        profit: "",
        date: ""
      },
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        id: [{ required: true, message: "请输入id", trigger: "blur" }],
        date: [{ required: true, message: "请输入时间", trigger: "blur" }],
        profit: [{ required: true, message: "请输入提成", trigger: "blur" }]
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    check() {
      this.tableData = [];
      var name = this.checkcontent.name;
      var time = this.checkcontent.dateValue;
      //console.log(name, time);

      if (name) {
        if (time) {
          this.totalData.forEach(item => {
            var date = new Date(item.date);
            if (item.name == name && time.getTime() === date.getTime()) {
              this.tableData.push(item);
            }
          });
        } else {
          this.totalData.forEach(item => {
            if (item.name == name) {
              this.tableData.push(item);
            }
          });
        }
      } else {
        if (time) {
          this.totalData.forEach(item => {
            var date = new Date(item.date);
            if (time.getTime() === date.getTime()) {
              this.tableData.push(item);
            }
          });
        } else {
          return this.$message({
            message: "请输入信息",
            type: "error"
          });
        }
      }
    },
    backtabel() {
      this.tableData = this.totalData.slice(0, this.page_size);
      this.checkcontent = { dateValue: "", name: "" };
    },
    tabelData() {
      this.tableData = this.totalData.slice(0, this.page_size);
      this.total = this.totalData.length;
    },
    handleCurrentChange(val) {
      console.log(val);
      this.tableData = this.totalData.slice(
        this.page_size * (val - 1),
        this.page_size * val
      );
    },
    edit(data) {
      this.ruleForm = data;
      this.dialogVisible = true;
    },
    async getTotalData() {
      await this.$http.get("../../static/json/profit.json").then(res => {
        this.totalData = res.data;
      });
      this.tabelData();
    },
    cancelForm() {
      this.ruleForm = {
        uid: "",
        id: "",
        name: "",
        profit: "",
        date: ""
      };
      this.dialogVisible = false;
      this.$message("您已取消编辑");
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.ruleForm);
          var uid = this.ruleForm.uid;
          this.totalData.forEach((item, index) => {
            if (item.uid == uid) {
              item == this.ruleForm;
            }
          });
          this.dialogVisible = false;
          this.tableData();
          return this.$message({
            message: "编辑信息成功",
            type: "success"
          });
        } else {
          return this.$message({
            message: "请您填写准确的信息",
            type: "warning"
          });
        }
      });
    },
    deleteUser(data) {
      this.$confirm("您确定要删除该用户信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          var billid = data.uid;
          this.totalData.forEach((item, index) => {
            if (item.uid == billid) {
              this.totalData.splice(index, 1);
            }
            this.tabelData();
          });
          this.tabelData();
          return this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          return this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },
  created() {},
  mounted() {
    this.getTotalData();
  },
  filters: {
    filterDate(date) {
      var date = new Date(date);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;

      var str = year + "年" + month + "月";
      return str;
    }
  }
};
</script>
<style lang="scss" scoped>
.wrapper {
}
.search {
  font-size: 16px;
  span {
    display: inline-block;
    margin-right: 17px;
  }
  .el-input {
    width: 200px;
  }
}
.clearfix {
  display: flex;
  justify-content: flex-start;
  div {
    margin-right: 50px;
  }
}
</style>
